﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

using Factonomy.AzureUtility.Core;
using Factonomy.AzureUtility.Core.Storage;
using Factonomy.AzureUtility.Core.StorageHelpers;

namespace Factonomy.AzureUtility.Controls
{
    class AccountContextMenu : BaseContextMenu
    {
        public event NoParameterDelegate AccountRemoved;
        public event NoParameterDelegate AccountAdded;
        private MenuItem _itemDisable = new MenuItem();

        protected virtual void OnAccountRemoved()
        {
            if (AccountRemoved != null)
                AccountRemoved();
        }

        protected virtual void OnAccountAdded()
        {
            if (AccountAdded != null)
                AccountAdded();
        }

        public AccountContextMenu()
        {
            MenuItem itemEdit = new MenuItem();
            itemEdit.Header = "Edit";
            itemEdit.Click += new System.Windows.RoutedEventHandler(OnItemEditClick);
            this.Items.Add(itemEdit);

            this._itemDisable.Header = "Disable";
            this._itemDisable.Click += new System.Windows.RoutedEventHandler(OnItemDisableClick);
            this.Items.Add(this._itemDisable);

            MenuItem itemRemove = new MenuItem();
            itemRemove.Header = "Remove";
            itemRemove.Click += new System.Windows.RoutedEventHandler(OnItemRemoveClick);
            this.Items.Add(itemRemove);

            this.Opened += new RoutedEventHandler(AccountContextMenu_Opened);

        }

        void AccountContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            try
            {
                //TreeViewItem item = (TreeViewItem)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
                TreeViewItem item = (TreeViewItem)((Popup)((ContextMenu)sender).Parent).PlacementTarget;
                if (item != null)
                {
                    if (item.Tag != null)
                    {
                        this._helper = item.Tag as TreeViewItemHelper;
                        ConfigAccount account = Config.Instance.GetAccount(this._helper.AccountName);
                        if (account.Enabled)
                        {
                            this._itemDisable.Header = "Disable";
                        }
                        else
                        {
                            this._itemDisable.Header = "Enable";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to open URI.  " + ex.Message);
            }
        }

        void OnItemDisableClick(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                TreeViewItem item = (TreeViewItem)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
                if (item != null)
                {
                    if (item.Tag != null)
                    {
                        this._helper = item.Tag as TreeViewItemHelper;
                        ConfigAccount account = Config.Instance.GetAccount(this._helper.AccountName);
                        if (((MenuItem)sender).Header.ToString() == "Disable")
                        {
                            Config.Instance.UpsertAccount(account.AccountName, account.SharedKey, account.Local, false);
                        }
                        else
                        {
                            Config.Instance.UpsertAccount(account.AccountName, account.SharedKey, account.Local, true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        void OnItemEditClick(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                TreeViewItem item = (TreeViewItem)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
                if (item != null)
                {
                    if (item.Tag != null)
                    {
                        this._helper = item.Tag as TreeViewItemHelper;
                        ConfigAccount account = Config.Instance.GetAccount(this._helper.AccountName);
                        Dialogs.ManageAccount accountDialog = new Dialogs.ManageAccount(account);
                        accountDialog.AccountChanged += new NoParameterDelegate(OnAccountChanged);
                        accountDialog.Owner = Application.Current.Windows[0];
                        accountDialog.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                this._helper = null;
            }
        }

        private void OnAccountChanged()
        {
            this.OnAccountAdded();
            this._helper = null;
        }

        void OnItemRemoveClick(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                TreeViewItem item = (TreeViewItem)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
                if (item != null)
                {
                    if (item.Tag != null)
                    {
                        this._helper = item.Tag as TreeViewItemHelper;

                        ConfigAccount account = Config.Instance.GetAccount(this._helper.AccountName);
                        if (MessageBox.Show("Really delete?", "Confirm delete", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                            Config.Instance.DeleteAccount(account.AccountName);
                            this.OnAccountRemoved();
                        }

                        this._helper = null;
                    }
                }
            }
            catch (Exception ex)
            {
                this._helper = null;
            }
        }


    }
}
